<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="领养人id" prop="aadoptCustomerId" style="width:190px">
        <el-input
          v-model="queryParams.aadoptCustomerId"
          placeholder="请输入领养人id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宠物id" prop="adoptPetId" style="width:190px">
        <el-input
          v-model="queryParams.adoptPetId"
          placeholder="请输入宠物id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="住房类型" prop="homeType">
        <el-select v-model="queryParams.homeType" placeholder="请选择住房类型" clearable style="width:146px">
          <el-option
            v-for="dict in house_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否允许回访" prop="adoptVisit" lable-width="300px">
        <el-select v-model="queryParams.adoptVisit" placeholder="请选择是否允许回访" clearable style="width:180px">
          <el-option
            v-for="dict in my_if"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否同意承诺" prop="adoptPromise">
        <el-select v-model="queryParams.adoptPromise" placeholder="请选择是否同意承诺" clearable style="width:180px">
          <el-option
            v-for="dict in my_if"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
        <el-form-item label="审核结果" prop="adoptPromise">
            <el-select v-model="queryParams.adoptStatus" placeholder="请选择" clearable style="width:180px">
                <el-option
                        label="申请中"
                        :value="0"
                />
                <el-option
                        label="已驳回申请"
                        :value="2"
                />
                <el-option
                        label="已通过审核"
                        :value="1"
                />
            </el-select>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="adoptList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="id" align="center" prop="adoptId" />
      <el-table-column label="领养人id" align="center" prop="aadoptCustomerId" />
      <el-table-column label="宠物id" align="center" prop="adoptPetId" />
      <el-table-column label="家庭成员人数" align="center" prop="familyNum" width="150"/>
      <el-table-column label="是否有小孩" align="center" prop="childrenHas" width="150">
        <template #default="scope" >
          <dict-tag :options="my_if" :value="scope.row.childrenHas"/>
        </template>
      </el-table-column>
      <el-table-column label="孩子年龄描述" align="center" prop="childrenDesc" width="250"/>
      <el-table-column label="是否有其他宠物" align="center" prop="petHas" width="150">
        <template #default="scope">
          <dict-tag :options="my_if" :value="scope.row.petHas"/>
        </template>
      </el-table-column>
      <el-table-column label="请说明宠物种类和数量(有宠物)" align="center" prop="petDesc" width="250"/>
      <el-table-column label="住房类型" align="center" prop="homeType">
        <template #default="scope">
          <dict-tag :options="house_type" :value="scope.row.homeType"/>
        </template>
      </el-table-column>
      <el-table-column label="是否允许回访" align="center" prop="adoptVisit" width="150">
        <template #default="scope">
                <el-button link type="primary"  v-if="scope.row.adoptVisit==1 && scope.row.adoptStatus==1">添加回访信息</el-button>
                <dict-tag :options="my_if" :value="scope.row.adoptVisit"  v-else />
        </template>
      </el-table-column>
      <el-table-column label="是否同意承诺" align="center" prop="adoptPromise" width="150">
        <template #default="scope">
          <dict-tag :options="my_if" :value="scope.row.adoptPromise"/>
        </template>
      </el-table-column>
      <el-table-column label="领养状态" align="center" prop="adoptStatus">
        <template #default="scope">
          <dict-tag :options="shelter_status" :value="scope.row.adoptStatus"/>
        </template>
      </el-table-column>
        <el-table-column label="审核反馈" align="center" prop="adoptResponse" width="250">
            <template #default="scope">
                <el-button link type="primary" v-if="scope.row.adoptStatus!=0&&scope.row.adoptResponse==null" @click="Fankui(scope.row)">添加反馈信息信息</el-button>
                <span v-else>{{scope.row.adoptResponse}}</span>
            </template>
        </el-table-column>

      <el-table-column label="申请时间" align="center" prop="adoptTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.adoptTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="驳回时间" align="center" prop="rejectTime" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.rejectTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="同意领养时间" align="center" prop="accessedTime" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.accessedTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
            <div v-if="scope.row.adoptStatus==0">
                <el-button link type="primary" icon="Check" @click="handleAgree(scope.row)" >同意</el-button>
                <el-button link type="primary" icon="Delete" @click="handleDisAgree(scope.row)" >驳回</el-button>
            </div>
            <el-button link type="primary"  v-if="scope.row.adoptStatus==1" >已通过审核</el-button>
            <el-button link type="primary" v-if="scope.row.adoptStatus==2" >已驳回申请</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改领养管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="adoptRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="领养人id" prop="aadoptCustomerId">
          <el-input v-model="form.aadoptCustomerId" placeholder="请输入领养人id" />
        </el-form-item>
        <el-form-item label="宠物id" prop="adoptPetId">
          <el-input v-model="form.adoptPetId" placeholder="请输入宠物id" />
        </el-form-item>
        <el-form-item label="家庭成员人数" prop="familyNum">
          <el-input v-model="form.familyNum" placeholder="请输入家庭成员人数" />
        </el-form-item>
        <el-form-item label="是否有小孩" prop="childrenHas">
          <el-select v-model="form.childrenHas" placeholder="请选择是否有小孩">
            <el-option
              v-for="dict in my_if"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="孩子年龄描述" prop="childrenDesc">
          <el-input v-model="form.childrenDesc" placeholder="请输入孩子年龄描述" />
        </el-form-item>
        <el-form-item label="是否有其他宠物" prop="petHas">
          <el-select v-model="form.petHas" placeholder="请选择是否有其他宠物">
            <el-option
              v-for="dict in my_if"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="请说明宠物种类和数量(有宠物)" prop="petDesc">
          <el-input v-model="form.petDesc" placeholder="请输入请说明宠物种类和数量(有宠物)" />
        </el-form-item>
        <el-form-item label="住房类型" prop="homeType">
          <el-select v-model="form.homeType" placeholder="请选择住房类型">
            <el-option
              v-for="dict in house_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否允许回访" prop="adoptVisit">
          <el-select v-model="form.adoptVisit" placeholder="请选择是否允许回访">
            <el-option
              v-for="dict in my_if"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否同意承诺" prop="adoptPromise">
          <el-select v-model="form.adoptPromise" placeholder="请选择是否同意承诺">
            <el-option
              v-for="dict in my_if"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="领养状态" prop="adoptStatus">
          <el-select v-model="form.adoptStatus" placeholder="请选择领养状态">
            <el-option
              v-for="dict in shelter_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核反馈" prop="adoptResponse">
          <el-input v-model="form.adoptResponse" placeholder="请输入审核反馈" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

<!--      //添加反馈信息的对话框-->
      <el-dialog  :title="title2" v-model="open2" width="500px" append-to-body>
          <el-form ref="disAgreeText" :model="from2" :rules="rules2" label-width="80px">
              <el-form-item label="反馈信息" prop="disAgreeText">
                  <el-input v-model="from2.disAgreeText"  placeholder="请输反馈信息" />
              </el-form-item>
          </el-form>
          <template #footer>
              <div class="dialog-footer">
                  <el-button type="primary" @click="submitForm2">确 定</el-button>
                  <el-button @click="cancel2">取 消</el-button>
              </div>
          </template>
      </el-dialog>
  </div>
</template>

<script setup name="Adopt">
import { listAdopt, getAdopt, delAdopt, addAdopt, updateAdopt,agreeAdopt ,disAgreeAdopt,addText} from "@/api/web/adopt";

const { proxy } = getCurrentInstance();
const { shelter_status, my_if, house_type } = proxy.useDict('shelter_status', 'my_if', 'house_type');

const adoptList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    aadoptCustomerId: null,
    adoptPetId: null,
    homeType: null,
    adoptVisit: null,
    adoptPromise: null,
      adoptStatus: null,
  },
  rules: {
  }
});
const { queryParams, form, rules } = toRefs(data);
/** 查询领养管理列表 */
function getList() {
  loading.value = true;
    console.log(queryParams.value)
  listAdopt(queryParams.value).then(response => {
    adoptList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
// 表单重置
function reset() {
  form.value = {
    adoptId: null,
    aadoptCustomerId: null,
    adoptPetId: null,
    familyNum: null,
    childrenHas: null,
    childrenDesc: null,
    petHas: null,
    petDesc: null,
    homeType: null,
    adoptVisit: null,
    adoptPromise: null,
    adoptStatus: null,
    adoptResponse: null,
    adoptTime: null,
    rejectTime: null,
    accessedTime: null
  };
  proxy.resetForm("adoptRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.adoptId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加领养管理";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _adoptId = row.adoptId || ids.value
  getAdopt(_adoptId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改领养管理";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["adoptRef"].validate(valid => {
    if (valid) {
      if (form.value.adoptId != null) {
        updateAdopt(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAdopt(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _adoptIds = row.adoptId || ids.value;
  proxy.$modal.confirm('是否确认删除领养管理编号为"' + _adoptIds + '"的数据项？').then(function() {
    return delAdopt(_adoptIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download('web/adopt/export', {
    ...queryParams.value
  }, `adopt_${new Date().getTime()}.xlsx`)
}


//=======================================================
// 同意领养申请
function handleAgree(row){
  const _adoptIds = row.adoptId;
  proxy.$modal.confirm('是否确认同意领养申请编号为"' + _adoptIds + '"的数据项？').then(function() {
    return agreeAdopt(_adoptIds,row.adoptPetId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("审批成功");
  }).catch(() => {});
}

//不同意领养
function handleDisAgree(row){
    const _adoptIds = row.adoptId;
    proxy.$modal.confirm('是否确认驳回领养申请编号为"' + _adoptIds + '"的数据项？').then(function() {
        return disAgreeAdopt(_adoptIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("驳回成功");
    }).catch(() => {});
}
//添加反馈信息
const from2 = reactive({
    disAgreeText:"",
    adoptId:null
})

rules2: {

}
const open2 = ref(false)
const title2 = ref("请输入驳回领养申请的原因")
function Fankui(row){
    from2.adoptId = row.adoptId
    open2.value=true;
}
function submitForm2(){
    console.log(from2)
    addText(from2.adoptId,from2.disAgreeText).then(res=>{
        cancel2()
        getList();
    })
}
function cancel2(){
    from2.adoptId = null
    from2.disAgreeText = ""
    proxy.resetForm("disAgreeText");
    open2.value = false;
}
getList();
</script>
